package com.servlet.user;

import com.pojo.User;
import com.service.user.UserService;
import com.service.user.UserServiceImpl;
import com.util.Info;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginServlet extends  HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String Ac = req.getParameter("userName");
        String Psw = req.getParameter("password");
        UserService userService = new UserServiceImpl();
        User user = userService.login(Ac,Psw);
        if(null!=user){
            HttpSession session=req.getSession();//登录成功 将用户账号密码存入session
            session.setAttribute("currentUser", user);
            resp.sendRedirect("main.jsp");
        }else{
            req.setAttribute("error","用户名或密码不正确");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
    }
}
